﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Mbs.Test.Model.Gui
{
    public class Control:MarshalByRefObject
    {
        public event EventHandler Click;

        public string Id { get; set; }
        public bool Enabled { get; set; }
        public bool Visible { get; set; }

        public void OnClick()
        {
            if (Click != null)
                Click(this, EventArgs.Empty);
        }

        public virtual void Render()
        {
            Console.WriteLine(ToString());
        }

        /// <summary>
        /// 可回收资源列表
        /// </summary>
        public ICompositeDisposableObject Disposes { get; private set; }

        public Control()
        {
            IsSupportGlobalization = true;
            Disposes = new CompositeDisposableObject();
        }

        /// <summary>
        /// 初始化视图
        /// </summary>
        public virtual void Init() { }

         private readonly object SynchRoot = new object();


       
        #region IDisposable Members

         ~Control()
         {
             Dispose(false);
         }

        public void Dispose()
        {
            lock (SynchRoot)
            {
                if (!IsDisposed)
                {
                    IsDisposed = true;
                    Disposes.Dispose();
                    Dispose(true);
                    GC.SuppressFinalize(this);
                }
            }
        }


        [Browsable(false)]
        public bool IsDisposed { get; private set; }

        protected virtual void Dispose(bool disposing)
        {
        }

        protected virtual void CheckNotDisposed()
        {
            if (IsDisposed)
                throw new ObjectDisposedException(GetType().Name);
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }


        #endregion

      

        /// <summary>
        /// 把视图上的UI元素上的相关事件附加到消息总线上
        /// </summary>
        /// <returns></returns>
        public virtual IEnumerator<IDisposable> Attach()
        {
            yield break;
        }

        /// <summary>
        /// 订阅消息总线发过来的消息
        /// </summary>
        /// <returns></returns>
        public virtual IEnumerator<IDisposable> Subscribe()
        {
            yield break;
        }

      

        /// <summary>
        /// 是否支持国际化
        /// </summary>
        public bool IsSupportGlobalization { get; set; }

        /// <summary>
        /// 初始化国际化资源
        /// </summary>
        public virtual void InitializeResource()
        {
        }

        /// <summary>
        /// 刷新国际化资源
        /// </summary>
        public virtual void RefreshResource()
        {
            if(IsSupportGlobalization)
                InitializeResource();
        }

 
    }
}
